// ============================================================================
//
// Graph framework by Aurelien Moreau aurelien.moreau@gmail.com
// Modified by Pierre CHATEL chatelp@gmail.com
// Distributed under LGPL
// Copyright (C) Aurelien Moreau, 2008, 2009. All rights reserved
//
// ============================================================================

package com.thalesgroup.graphFramework.collections.iterator;

import java.util.Iterator;

public final class IteratorList<T> implements Iterator<T> {

	protected Iterator<Iterator<? extends T>> iterator;

	protected Iterator<? extends T>[] tab;

	protected int index;

	protected Iterator<? extends T> current;

	public IteratorList(final Iterable<Iterator<? extends T>> iterators) {
		iterator = iterators.iterator();
		current = iterator.next();
		nextInIterator();
	}

	public IteratorList(final Iterator<? extends T>... iterators) {
		tab = iterators;
		index = 0;
		current = tab[0];
		nextInTab();
	}

	public boolean hasNext() {
		return current.hasNext();
	}

	protected void nextInIterator() {
		if (!current.hasNext()) {
			while (iterator.hasNext()) {
				current = iterator.next();
				if (current.hasNext()) {
					return;
				}
			}
		}
	}

	protected void nextInTab() {
		if (!current.hasNext()) {
			while (index < tab.length) {
				current = tab[index++];
				if (current.hasNext()) {
					return;
				}
			}
		}
	}

	public T next() {
		final T t = current.next();
		if (tab != null) {
			nextInTab();
		} else {
			nextInIterator();
		}
		return t;
	}

	public void remove() {
		throw new UnsupportedOperationException();
	}

}
